using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using nDeveloper.BO.Base.Domain;
using nDeveloper.BO.Info;
using nDeveloper.UI.Base.Windows.Common;

namespace nDeveloper.UI.Windows.IDE
{
    public partial class frmAddDefaultData : Form
    {
        ArrayList _arCampos;
        DataSet _dsDados;
        string _entityName;
        int _selectedIndex = -1;

        public int SelectedIndex {
            set {
                _selectedIndex = value;
            }
        }

        public string EntityName {
            set {
                _entityName = value;
            }
        }

        public DataSet dsDadosPadroes {
            set {
                _dsDados = value;
            }
        }

        public ArrayList ListaCampos {
            set {
                _arCampos = value; 
            }
        }

        public frmAddDefaultData() {
            InitializeComponent();
        }

        private void frmAddDefaultData_Load(object sender, EventArgs e) {

            int column = 0, row = 0;

            tblLayout.ColumnCount = 2;

            foreach (SystemDataDictionaryInfoEntityColumns col in _arCampos) {

                // adiciona um label
                Label lblControl = new Label();
                lblControl.TextAlign = ContentAlignment.MiddleLeft;
                lblControl.Dock = DockStyle.Fill;
                lblControl.Text = col.FriendlyName;
                lblControl.AutoSize = true;
                tblLayout.Controls.Add(lblControl, column, row);
                column++;

                // adiciona um campo                
                switch (col.DataType) {
                    case "Varchar":
                        TextBox txtControl = new TextBox();
                        txtControl.Name = "txt" + col.Name;
                        txtControl.Width = 400;
                        tblLayout.Controls.Add(txtControl, column, row);
                        break;
                    case "Date":
                        DateTimePicker dtControl = new DateTimePicker();
                        dtControl.Name = "txt" + col.Name;
                        dtControl.Width = 100;
                        dtControl.Format = DateTimePickerFormat.Short;
                        tblLayout.Controls.Add(dtControl, column, row);
                        break;
                    case "Time":
                        DateTimePicker tmControl = new DateTimePicker();
                        tmControl.Name = "txt" + col.Name;
                        tmControl.Width = 100;
                        tmControl.Format = DateTimePickerFormat.Time;
                        tblLayout.Controls.Add(tmControl, column, row);
                        break;
                    case "BlobText":
                        TextBox btControl = new TextBox();
                        btControl.Name = "txt" + col.Name;
                        btControl.Multiline = true;
                        btControl.Width = 300;
                        btControl.Height = 50;
                        tblLayout.Controls.Add(btControl, column, row);
                        break;
                    default:
                        TextBox txtDefaultControl = new TextBox();
                        txtDefaultControl.Name = "txt" + col.Name;
                        txtDefaultControl.Width = 100;
                        tblLayout.Controls.Add(txtDefaultControl, column, row);
                        break;
                }
                                
                column++;
                                
                                
                if (column == tblLayout.ColumnCount) {
                    row++;
                    column = 0;
                    tblLayout.RowCount = row;
                }
            }

            tblLayout.AutoSize = true;

            if (_selectedIndex != -1) {
                DataRow dr = _dsDados.Tables[0].Rows[_selectedIndex];

                foreach (Control ctrl in tblLayout.Controls) {
                    if (ctrl.Name.StartsWith("txt")) {
                        ctrl.Text = dr[ctrl.Name.Substring(3)].ToString();
                    }
                }
            }
        }

        private void Salvar() {
            DataRow dr = null;

            if (_selectedIndex == -1)
                dr = _dsDados.Tables[0].NewRow();
            else
                dr = _dsDados.Tables[0].Rows[_selectedIndex];

            foreach (Control ctrl in tblLayout.Controls) {
                if (ctrl.Name.StartsWith("txt")) {
                    dr[ctrl.Name.Substring(3)] = ctrl.Text;
                }
            }

            if (_selectedIndex == -1)
                _dsDados.Tables[0].Rows.Add(dr);

            SystemDataDictionaryDefaultValue def = new SystemDataDictionaryDefaultValue();
            def.Save(_dsDados, _entityName);
        }

        private void btnSalvarFechar_Click(object sender, EventArgs e) {
            Salvar();
            DialogResult = DialogResult.OK;
        }

        private void btnSalvarNovo_Click(object sender, EventArgs e) {
            Salvar();
            Tela.LimpaCampos(this.tblLayout.Controls);
        }

        private void btnCancelar_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.OK;
        }
    }
}